home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / source / src / plane / _line.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  2.2 KB  |  111 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  _line.c
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16. #include <LEDA/line.h>
  17. #include <math.h>
  18.  
  19. static const double eps = 1e-10;
  20.  
  21. //------------------------------------------------------------------------------
  22. // lines 
  23. //------------------------------------------------------------------------------
  24.  
  25.  
  26.  
  27. line::line()
  28. { PTR = new line_rep; }
  29.  
  30. line::line(const segment& s) 
  31. { PTR = new line_rep(s); }
  32.  
  33. line::line(const point& x, const point& y)    
  34. { PTR = new line_rep(segment(x,y)); }
  35.  
  36. line::line(const point& p, double alpha) 
  37. { PTR = new line_rep(segment(p,alpha,1)); }
  38.   
  39.  
  40.  
  41. bool line::contains(const point& p)   const
  42. { if (vertical())
  43.       return p.xcoord() == ptr()->seg.xcoord1(); 
  44.   else
  45.       return p.ycoord() == y_proj(p.xcoord());
  46.  }
  47.  
  48. bool line::contains(const segment& s) const
  49. { point p = s.start(); 
  50.   point q = s.end();
  51.   if (contains(p))
  52.     if (contains(q)) return true; 
  53.   return false;
  54.  }
  55.  
  56.  
  57. ostream& operator<<(ostream& out, const line& l) 
  58. { return out << l.seg(); }
  59.  
  60. istream& operator>>(istream& in, line& l)  
  61. { segment s; 
  62.   in >> s; 
  63.   l = line(s); 
  64.   return in; 
  65.  }
  66.  
  67.  
  68. bool line::intersection(const line& s, point& inter) const
  69. { double cx,cy;
  70.  
  71.   if (slope() == s.slope()) return false;
  72.  
  73.   if (vertical())
  74.      cx = ptr()->seg.xcoord1();
  75.   else
  76.      if (s.vertical())
  77.         cx = s.ptr()->seg.xcoord1();
  78.      else
  79.         cx = (s.y_abs()-y_abs())/(slope()-s.slope());
  80.  
  81.   if (vertical())
  82.      cy = slope() * cx + y_abs();
  83.   else
  84.      cy = s.slope() * cx + s.y_abs();
  85.  
  86.   inter = point(cx,cy);
  87.  
  88.   return true;
  89. }
  90.  
  91.  
  92. bool line::intersection(const segment& s, point& inter) const
  93. { if (intersection(line(s),inter))
  94.   { double d1  = inter.distance(s.ptr()->start);
  95.     double d2  = inter.distance(s.ptr()->end);
  96.     double l   = s.length();
  97.     if ( d1<=l && d2 <=l) return true;
  98.    }
  99.    return false;
  100. }
  101.  
  102.  
  103. segment line::perpendicular(const point& q) const
  104. { segment s = ptr()->seg;
  105.   point r = q.translate(s.angle()+M_PI_2,-distance(q));
  106.   return segment(q,r);
  107.  }
  108.  
  109.  
  110.